package com.zdlog.wenjuan.service.impl;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.dao.ProblemDetailRepository;
import com.zdlog.wenjuan.dao.ProblemOptionRepository;
import com.zdlog.wenjuan.dataobject.ProblemDetail;
import com.zdlog.wenjuan.dataobject.ProblemOption;
import com.zdlog.wenjuan.enums.ProblemTypeEnum;
import com.zdlog.wenjuan.service.ProblemService;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.service.impl
 * @author: Kuaile
 * @create: 2018-02-06 21:24
 **/
@Service
public class ProblemServiceImpl implements ProblemService {


    @Autowired
    private ProblemOptionRepository optionRepository;


    @Autowired
    private ProblemDetailRepository detailRepository;


    @Override
    @Transactional
    public Boolean addProblem(ProblemDetail problemDetail, List<ProblemOption> option) {
        List<ProblemOption> result = new ArrayList<>();
        for (ProblemOption obj : option) {
            result.add(optionRepository.save(obj));
        }

        ProblemDetail resultDetail = detailRepository.save(problemDetail);
        if (resultDetail != null && result.size() == option.size()) {
            return true;
        }
        return false;
    }

    @Override
    public ProblemDetail findByProblemId(String problemId) {
        return detailRepository.findByProblemDetailId(problemId);
    }

    @Override
    public ResultBoolenVO delete(String problemId) {
        detailRepository.delete(problemId);
        List<ProblemOption> problemOptionList = optionRepository.findByProblemIdOrderBySerialNumberAsc(problemId);
        if (problemOptionList.size() != 0) {
            for (ProblemOption problemOption : problemOptionList) {
                optionRepository.delete(problemOption.getOptionId());
            }
        }
        return ResultBoolenVOUtil.success();
    }

    @Override
    public Boolean updataProblem(ProblemDetail problemDetail, List<ProblemOption> option) {
        ProblemDetail detail = detailRepository.findByProblemDetailId(problemDetail.getProblemDetailId());
        problemDetail.setSerialNumber(detail.getSerialNumber());
        BeanUtils.copyProperties(problemDetail, detail);
        detailRepository.save(detail);
        for (ProblemOption op : optionRepository.findByProblemIdOrderBySerialNumberAsc(problemDetail.getProblemDetailId())) {
            optionRepository.delete(op.getOptionId());
        }
        if (problemDetail.getProblemType() == ProblemTypeEnum.DANXUAN.getCode() || problemDetail.getProblemType() == ProblemTypeEnum.DUOXUAN.getCode()) {
            if (option.size() != 0) {
                for (ProblemOption problemOption : option) {
                    optionRepository.save(problemOption);
                }
            }
        }
        return true;
    }

}
